package ngames.com.sushitv.temp.Module;

import android.os.Handler;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;

import com.example.myvolley.MyJsonObjectRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.entity.Anime;

/**
 * Created by edmond on 16-7-28.
 */
public class AnimeFactory {
    public void initData(AppApplication application ,final List<Anime> animesList, final Handler handler){
        Map<String,String> stringRequest = new HashMap();
        stringRequest.put("page","1");
        stringRequest.put("size","4");
        stringRequest.put("uuid","xxx");
        stringRequest.put("timestamp","1470104998");
        stringRequest.put("device","android");
        stringRequest.put("api_version","1.0");
        stringRequest.put("app_version","1.0");
        stringRequest.put("token",application.token);

        MyJsonObjectRequest jsonObjectRequest = new MyJsonObjectRequest("http://60.191.1.98:9080/v1/subscription/sub-dramas", stringRequest, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.d("AnimeFactoryInit", String.valueOf(response));
                try {
                    JSONArray jsonArray = response.getJSONArray("data");
                    List<Anime> animeList = JSON.parseArray(jsonArray.toString(),Anime.class);
                    Log.d("AnimeFactoryInit", String.valueOf(animeList.size()));
                    animesList.clear();
                    animesList.addAll(animeList);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                handler.sendEmptyMessage(0);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        if(application.queue == null){
            application.queue = Volley.newRequestQueue(application);
        }

        application.queue.add(jsonObjectRequest);
    }

    public void refreshData(AppApplication application,final List<Anime> animesList, final Handler handler){
        Map<String,String> stringRequest = new HashMap();
        stringRequest.put("page","1");
        stringRequest.put("size","4");
        stringRequest.put("uuid","xxx");
        stringRequest.put("timestamp","1470104998");
        stringRequest.put("device","android");
        stringRequest.put("api_version","1.0");
        stringRequest.put("app_version","1.0");
        stringRequest.put("token",application.token);

        MyJsonObjectRequest jsonObjectRequest = new MyJsonObjectRequest("http://60.191.1.98:9080/v1/subscription/sub-dramas", stringRequest, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                animesList.clear();
                Log.d("AnimeFactoryRefresh", String.valueOf(response));
                try {
                    JSONArray jsonArray = response.getJSONArray("data");
                    List<Anime> animeList = JSON.parseArray(jsonArray.toString(),Anime.class);
                    Log.d("AnimeFactoryInit", String.valueOf(animeList.size()));
                    animesList.clear();
                    animesList.addAll(animeList);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                handler.sendEmptyMessage(2);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        if(application.queue == null){
            application.queue = Volley.newRequestQueue(application);
        }

        application.queue.add(jsonObjectRequest);
    }

    public void moreData(AppApplication application,final List<Anime> animesList,final Handler handler,int page){
        Map<String,String> stringRequest = new HashMap();
        stringRequest.put("page",page+"");
        stringRequest.put("size","4");
        stringRequest.put("uuid","xxx");
        stringRequest.put("timestamp","1470104998");
        stringRequest.put("device","android");
        stringRequest.put("api_version","1.0");
        stringRequest.put("app_version","1.0");
        stringRequest.put("token",application.token);

        MyJsonObjectRequest jsonObjectRequest = new MyJsonObjectRequest("http://60.191.1.98:9080/v1/subscription/sub-dramas", stringRequest, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.d("AnimeFactoryMore", String.valueOf(response));
                try {
                    JSONArray jsonArray = response.getJSONArray("data");
                    List<Anime> animeList = JSON.parseArray(jsonArray.toString(),Anime.class);
                    Log.d("AnimeFactoryInit", String.valueOf(animeList.size()));
                    animesList.addAll(animeList);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                handler.sendEmptyMessage(1);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        if(application.queue == null){
            application.queue = Volley.newRequestQueue(application);
        }
        application.queue.add(jsonObjectRequest);
    }
}
